package com.com.apitools.ui.dialog;

import com.com.apitools.ui.config.Configuration;
import com.com.apitools.ui.utils.StringUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * ***************************************************************
 * <p/>
 * <pre>
 * Copyright (c) 2014 –苏州犀牛网络科技有限公司
 *  Title: com.com.apitools.ui.dialog
 *  Description:
 * ***************************************************************
 *  8/31 0031  V1.0  xiniu    New Files for com.com.apitools.ui.dialog
 * </pre>
 */
public class SettingDialog extends JDialog {
    private final JPanel contentPanel = new JPanel();
    private JTextField contractFile;
    private JTextField package_company;
    private JTextField author;
    private JTextField company;
    private Configuration configuration;

    public SettingDialog(Configuration configuration) {
        setModal(true);
        this.configuration = configuration;
        setBounds(100, 100, 578, 252);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);

        JLabel contractFileLabel = new JLabel("契约文件");
        contractFileLabel.setBounds(10, 25, 58, 14);
        contentPanel.add(contractFileLabel);

        contractFile = new JTextField();
        contractFile.setBounds(78, 22, 378, 20);
        contentPanel.add(contractFile);
        contractFile.setColumns(10);
        contractFile.disable();
        JButton selectFile = new JButton("选择文件");
        selectFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jfc.setFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f.getName().endsWith(".java") || f.isDirectory()) {
                            return true;
                        }
                        return false;
                    }
                    @Override
                    public String getDescription() {
                        return "契约文件(*.java)";
                    }
                });
                jfc.setCurrentDirectory(new File(contractFile.getText()));
                jfc.showDialog(new JLabel(), "请选择契约文件");
                File file = jfc.getSelectedFile();
                if(file!=null){
                    contractFile.setText(file.getPath());
                }
            }
        });
        selectFile.setBounds(466, 21, 89, 23);
        contentPanel.add(selectFile);

        JLabel authorLabel = new JLabel("作   者");
        authorLabel.setBounds(10, 66, 58, 14);
        contentPanel.add(authorLabel);

        author = new JTextField();
        author.setBounds(78, 63, 378, 20);
        contentPanel.add(author);
        author.setColumns(10);

        JLabel companyLabel = new JLabel("公   司");
        companyLabel.setBounds(10, 97, 64, 14);
        contentPanel.add(companyLabel);

        company = new JTextField();
        company.setColumns(10);
        company.setBounds(78, 94, 378, 20);
        contentPanel.add(company);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("\u786E\u5B9A");
                okButton.setActionCommand("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //设置
                        okButtonClick(e);
                    }
                });
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("\u53D6\u6D88");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
                cancelButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
            }
        }
        loadConfiguration();
        centerScreen();
    }

    public void centerScreen() {
        Dimension dim = getToolkit().getScreenSize();
        Rectangle abounds = getBounds();
        setLocation((dim.width - abounds.width) / 2, (dim.height - abounds.height) / 2);
    }

    private void loadConfiguration() {
        contractFile.setText(configuration.getContractFile());
//        package_company.setText(configuration.getContractName());
        author.setText(configuration.getAuthor());
        company.setText(configuration.getCompany());
    }

    /**
     * 设置数据
     *
     * @param e
     */
    private void okButtonClick(ActionEvent e) {
        String contractfile = contractFile.getText();
        if (StringUtil.isEmpty(contractfile)) {
            JOptionPane.showMessageDialog(this, "请选择契约文件.", "提示", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
//        String contractNameStr = package_company.getText();
//        if (StringUtil.isEmpty(contractNameStr)) {
//            JOptionPane.showMessageDialog(this, "请输入契约名称.", "提示", JOptionPane.INFORMATION_MESSAGE);
//            return;
//        }
        String authorStr = author.getText();
        String compayStr = company.getText();
        configuration.setContractFile(contractfile);
//        configuration.setContractName(contractPathStr);
        if (!StringUtil.isEmpty(authorStr)) {
            configuration.setAuthor(authorStr);
        }
        if (!StringUtil.isEmpty(compayStr)) {
            configuration.setCompany(compayStr);
        }
        configuration.save();
        dispose();
    }
}
